package com.bra;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.bra.chat.DemoHelper;
import com.bra.utils.SPHelper;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.umeng.socialize.PlatformConfig;
import android.app.Activity;
import android.app.Application;
import android.content.Context;

/**
 * 项目application
 * @author lenovo
 *
 */
public class BraApplication extends Application {
	public static BraApplication instance;
	private static List<Activity> activitys = new LinkedList();
	
    public static Context appContext;
    public final String PREF_USERNAME = "username";
    /**
	 * 当前用户nickname,为了苹果推送不是userid而是昵称
	 */
	public static String currentUserNick = "";
	public static Context context;
	public LocationClient mLocationClient = null;
	public MyLocationListenner myListener = new MyLocationListenner();
	private double Latitude,Longitude;
	@Override
	public void onCreate() {
		super.onCreate();
		appContext = this;
		this.context = getApplicationContext();
		// 百度地图初始化
		SDKInitializer.initialize(getApplicationContext());
		// 微信 appid appsecret
		PlatformConfig.setWeixin("wx967daebe835fbeac","5bb696d9ccd75a38c8a0bfe0675559b3");
		// 新浪微博 appkey appsecret
		PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad");
		 // QQ和Qzone appid appkey 
		PlatformConfig.setQQZone("100424468","c7394704798a158208a74ab60104f0ba");
		
		
	   DemoHelper.getInstance().init(appContext);
	   //极光 初始化
	   JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
	   JPushInterface.init(this);        // 初始化 JPush
	   //imagelode初始化
	   initImagerLoader();
	   mLocationClient = new LocationClient(this);
	   mLocationClient.registerLocationListener(myListener);
	   setLocationOption();
	   mLocationClient.start();

	}
	// 设置相关参数
			private void setLocationOption() {
				LocationClientOption option = new LocationClientOption();
				option.setOpenGps(true); // 打开gps
				option.setServiceName("com.baidu.location.service_v2.9");
				option.setAddrType("all");
				option.setPriority(LocationClientOption.NetWorkFirst);
				option.setPriority(LocationClientOption.GpsFirst);
				option.disableCache(true);
				mLocationClient.setLocOption(option);
			}
			public class MyLocationListenner implements BDLocationListener {
				@Override
				public void onReceiveLocation(BDLocation location) {						
					if (location == null) {
						return;
					}		
					Latitude=location.getLatitude();		
					Longitude=location.getLongitude();				
					SPHelper.saveString(appContext, "Latitude", Latitude+"");
					SPHelper.saveString(appContext, "Longitude", Longitude+"");
				 }
				public void onReceivePoi(BDLocation arg0) {
				}
			}
	public static BraApplication getInstance() {
		if (instance == null) {
			instance = new BraApplication();
		}
		return instance;
	}
	private void onDestroy() {
		mLocationClient.stop();
	}
	// 添加Activity到容器中
	public void addActivitys(Activity activity) {
		activitys.add(activity);
	}

	// 遍历所有Activity并finish
	public void exits() {
		for (Activity activity : activitys) {
			activity.finish();
		}
	}
	/**
	 * imageloader 初始化
	 */
	@SuppressWarnings("deprecation")
	private void initImagerLoader() {
		File cacheDir = StorageUtils.getOwnCacheDirectory(context,
				"imageloader/Cache");
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				context)
				.memoryCacheExtraOptions(480, 800)
				// max width, max height，即保存的每个缓存文件的最大长宽
				.threadPoolSize(3)
				// 线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
				// You can pass your own memory cache
				// implementation/你可以通过自己的内存缓存实现
				.memoryCacheSize(2 * 1024 * 1024)
				.discCacheSize(50 * 1024 * 1024)
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				// 将保存的时候的URI名称用MD5 加密
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.discCacheFileCount(100)
				// 缓存的文件数量
				.discCache(new UnlimitedDiskCache(cacheDir))
				// 自定义缓存路径
				.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
				.imageDownloader(
						new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout
				.writeDebugLogs() // Remove for release app
				.build();// 开始构建
		ImageLoader.getInstance().init(config);
	}
	
}
